package com.tttshaoqi.server.controller;

import com.tttshaoqi.annotation.Log;
import com.tttshaoqi.common.result.PageResult;
import com.tttshaoqi.common.result.Result;
import com.tttshaoqi.common.constant.RedisConstants;
import com.tttshaoqi.common.utils.EmpHolder;
import com.tttshaoqi.pojo.dto.LoginDTO;
import com.tttshaoqi.pojo.dto.PassWordDTO;
import com.tttshaoqi.pojo.entity.Employee;
import com.tttshaoqi.server.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 员工登录接口
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO loginDTO){
        // log.info("员工登录:{}",loginDTO);
        return employeeService.login(loginDTO);
    }

    /**
     * 分页查询员工接口
     */
    @GetMapping("/list")
    public Result list(Integer page,Integer pageSize,String name){
        log.info("员工分页查询：page:{},pageSize:{},name:{}",page,pageSize,name);
        PageResult pageResult = employeeService.page(page,pageSize,name);
        return Result.success(pageResult);
    }

    /**
     * 启用或禁用员工
     */
    @Log
    @PostMapping("/status/{status}")
    public Result status(@PathVariable Integer status,Long id){
        log.info("启用或禁用员工：{},{}",id,status==1 ? "启用" : "禁用");
        employeeService.status(status,id);
        return Result.success();
    }

    /**
     * 添加员工信息
     */
    @Log
    @PostMapping("/add")
    public Result addEmp(@RequestBody Employee employee){
        log.info("添加员工信息：{}",employee);
        return employeeService.add(employee);
    }

    /**
     * 根据id查询员工信息
     */
    @GetMapping("/getById")
    public Result getById(Long id){
        log.info("根据id查询员工信息：{}",id);
        return employeeService.getById(id);
    }

    /**
     * 修改员工信息
     */
    @Log
    @PutMapping()
    public Result update(@RequestBody Employee employee){
        log.info("修改员工信息:{}",employee);
        employeeService.update(employee);
        return Result.success();
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public Result logoout(String token){
        log.info("退出登录");
        String key = RedisConstants.LOGIN_USER_KEY+token;
        EmpHolder.removeEmp();
        stringRedisTemplate.delete(key);
        return Result.success();
    }

    /**
     * 修改密码
     */
    @Log
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody PassWordDTO passWordDTO){
        log.info("修改密码:{}",passWordDTO);
        return employeeService.editPassword(passWordDTO);
    }
}
